package com.lisy.demo.activity;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

import com.lisy.library.container.FragmentContainer;
import com.lisy.library.container.FragmentContainerDelegater;
import com.lisy.library.container.NodeFragment;
import com.lisy.demo.fragment.FirstContainerFragment;
import com.lisy.library.R;

import java.util.List;

public class FragmentContatinerActivity extends FragmentActivity implements FragmentContainerDelegater {
    private FragmentContainer fragmentContainer;
    private static final int MAX_FRAGMENT_CACHE_COUNT = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_container);
        initFragmentContainer();
    }


    @Override
    public void initFragmentContainer() {
        FragmentTransaction t = getSupportFragmentManager().beginTransaction();
        List<Fragment> fragmentList = getSupportFragmentManager().getFragments();
        if (fragmentList != null) {
            for (Fragment fragment : fragmentList) {
                if (fragment != null) {
                    t.remove(fragment);
                }
            }
        }
        t.commit();

        fragmentContainer = (FragmentContainer) findViewById(R.id.fragment_container);
        fragmentContainer.setOffscreenPageLimit(MAX_FRAGMENT_CACHE_COUNT);
        fragmentContainer.init(this);
        fragmentContainer.setHorizontalFadingEdgeEnabled(false);
        fragmentContainer.setFadingEdgeLength(0);

        fragmentContainer.addFragment(FirstContainerFragment.class, null,
                NodeFragment.INVALID_REQUEST_CODE, true, false);
    }

    @Override
    public FragmentContainer getFragmentContainer() {
        return fragmentContainer;
    }
}
